//
//  DHCommunityDetailController.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/1.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHCommunityDetailController.h"
#import "DHCommunity_NoImageCell.h"
#import "DHNewsDetail_CommentCell.h"
#import "XHInputView.h"

@interface DHCommunityDetailController ()<UITableViewDelegate, UITableViewDataSource, XHInputViewDelagete>

@property (strong, nonatomic) NSDictionary *communityDic; //朋友圈状态
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *addCommentView;
@property (weak, nonatomic) IBOutlet UIView *bottoView;

@property (strong, nonatomic) NSArray *hotArr;
@property (strong, nonatomic) NSMutableArray *latestArr;
@property (assign, nonatomic) NSInteger pageIndex;

@end

@implementation DHCommunityDetailController

- (instancetype)initWithCommunityDic:(NSDictionary *)communityDic
{
    self = [super init];
    if (self) {
        self.communityDic = communityDic;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"详情";
    
    self.addCommentView.layer.cornerRadius = self.addCommentView.height / 2;
    self.addCommentView.layer.borderWidth = 1;
    self.addCommentView.layer.borderColor = [[[UIColor colorWithHexString:@"#707070"] colorWithAlphaComponent:0.22] CGColor];
    self.bottoView.layer.shadowOpacity = 0.1;
    self.bottoView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.bottoView.layer.shadowRadius = 2;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DHCommunity_NoImageCell" bundle:nil] forCellReuseIdentifier:kDHCommunity_NoImageCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHNewsDetail_CommentCell" bundle:nil] forCellReuseIdentifier:kDHNewsDetail_CommentCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewComments)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreNewComments)];

    
    [self loadHotComments];
    [self loadNewComments];
}

- (void)loadHotComments{
    [[DHNetAPIClient shareDHNetAPIClient] request_social_post_comments_simpleWithParams:@{@"post_id":[self.communityDic objectForKey:@"_id"]} andBlock:^(id data, NSError *error) {
        self.hotArr = [data objectForKey:@"hotest"];
        [self.tableView reloadRow:1 inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }];
}

- (void)loadNewComments{
    self.pageIndex = 0;
    [self.latestArr removeAllObjects];
    [self.tableView.mj_footer resetNoMoreData];
    [[DHNetAPIClient shareDHNetAPIClient] request_social_posts_commentsWithParams:@{@"post_id":[self.communityDic objectForKey:@"_id"],@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.latestArr addObjectsFromArray:[data objectForKey:@"comments"]];
        if (self.latestArr.count < 20) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.pageIndex += 1;
        }
        [self.tableView reloadRow:2 inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }];
}

- (void)loadMoreNewComments{
    [self loadHotComments];
    [[DHNetAPIClient shareDHNetAPIClient] request_social_posts_commentsWithParams:@{@"post_id":[self.communityDic objectForKey:@"_id"],@"pageIndex":@(self.pageIndex),@"pageSize":@(20)} andBlock:^(id data, NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [self.latestArr addObjectsFromArray:[data objectForKey:@"comments"]];
        if (self.latestArr.count < 20 * (self.pageIndex + 1)) {
            self.tableView.mj_footer.hidden = YES;
        }else{
            self.pageIndex += 1;
        }
        [self.tableView reloadRow:2 inSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }];
}

- (void)postCommentWithString:(NSString *)commentStr{
    [[DHNetAPIClient shareDHNetAPIClient] request_social_post_commentWithParams:@{@"post_id":[self.communityDic objectForKey:@"_id"],@"content":commentStr} andBlock:^(id data, NSError *error) {
        [self loadNewComments];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addComment:(id)sender {
    [XHInputView showWithStyle:InputViewStyleDefault configurationBlock:^(XHInputView *inputView) {
        /** 请在此block中设置inputView属性 */
        
        /** 代理 */
        inputView.delegate = self;
        
        /** 占位符文字 */
        inputView.placeholder = @"请输入评论文字...";
        /** 设置最大输入字数 */
        inputView.maxCount = 50;
        /** 输入框颜色 */
        inputView.textViewBackgroundColor = [UIColor groupTableViewBackgroundColor];
        
        /** 更多属性设置,详见XHInputView.h文件 */
        
    } sendBlock:^BOOL(NSString *text) {
        if(text.length){
            NSLog(@"输入的信息为:%@",text);
            [self postCommentWithString:text];
            return YES;//return YES,收起键盘
        }else{
            [NSObject showHudTipStr:@"请输入评论内容"];
            return NO;//return NO,不收键盘
        }
    }];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        NSArray *images = [self.communityDic objectForKey:@"images"];
        
        //计算文字高度
        CGFloat contentHeight = [[self.communityDic objectForKey:@"content"] heightForFont:[UIFont systemFontOfSize:14] width:kScreenWidth - 20];
        //如果内容中有 \n 等转义字符
        NSInteger specialChar = 0;//出现次数
        specialChar = [[self.communityDic objectForKey:@"content"] countOccurencesOfString:@"\n"];
        CGFloat textHeight = contentHeight + specialChar * 14;
        
        //图片CollectionView高度
        CGFloat imgsHeight = 0;
        if (images.count == 0) {
            imgsHeight = 0;
        }else if (images.count > 0 && images.count <= 3){
            imgsHeight = 128;
        }else if (images.count > 3 && images.count <= 6){
            imgsHeight = 128 * 2 + 5;
        }else if (images.count > 6 && images.count <= 9){
            imgsHeight = 128 * 3 + 10;
        }
        return imgsHeight + textHeight + 133;
    }else if (indexPath.row == 1){
        if (self.hotArr.count > 0) {
            return self.hotArr.count * 147 + 40;
        }else{
            return 0;
        }
    }else if (indexPath.row == 2){
        if (self.latestArr.count > 0) {
            return self.latestArr.count * 147 + 40;
        }else{
            return 0;
        }
    }else{
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        DHCommunity_NoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHCommunity_NoImageCell forIndexPath:indexPath];
        [cell updateCell:self.communityDic];
        return cell;
    }else if (indexPath.row == 1){
        DHNewsDetail_CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHNewsDetail_CommentCell forIndexPath:indexPath];
        cell.titleLabel.text = @"热门评论";
        cell.allView.hidden = YES;
        [cell updateCellArr:self.hotArr];
        cell.isCommunityComment = YES;
        return cell;
    }else if (indexPath.row == 2){
        DHNewsDetail_CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHNewsDetail_CommentCell forIndexPath:indexPath];
        cell.titleLabel.text = @"最新评论";
        cell.allView.hidden = YES;
        cell.articleID = [self.communityDic objectForKey:@"_id"];
        cell.isCommunityComment = YES;
        [cell updateCellArr:self.latestArr];
        return cell;
    }else{
        return nil;
    }
}

#pragma mark - XHInputViewDelagete
/** XHInputView 将要显示 */
-(void)xhInputViewWillShow:(XHInputView *)inputView{
    
    /** 如果你工程中有配置IQKeyboardManager,并对XHInputView造成影响,请在XHInputView将要显示时将其关闭 */
    
    //[IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    //[IQKeyboardManager sharedManager].enable = NO;
    
}

/** XHInputView 将要影藏 */
-(void)xhInputViewWillHide:(XHInputView *)inputView{
    
    /** 如果你工程中有配置IQKeyboardManager,并对XHInputView造成影响,请在XHInputView将要影藏时将其打开 */
    
    //[IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    //[IQKeyboardManager sharedManager].enable = YES;
}

#pragma mark - arr init

- (NSArray *)hotArr{
    if (!_hotArr) {
        _hotArr = [NSArray array];
    }
    return _hotArr;
}

- (NSMutableArray *)latestArr{
    if (!_latestArr) {
        _latestArr = [NSMutableArray array];
    }
    return _latestArr;
}

@end
